$(document).ready(function(){
	var evaporationPanelDate = $('.evaporation-panel-date');
	var evaporationDataTable = $('#evaporationDataTable');
	var outSide = $('#outSideEvaporation');
	var hourTime_vertical = $('#hourTime_vertical');
	showInProvOrCity();
	var gisRains;
	var evaporation = $('#evaporation');
	hourTime_vertical.TouchSpin({
      verticalbuttons: true,
      min: 1,
      max: 23,
      initval:new Date().getHours()
    });
	var layer = $.map.layer,
	txtLayer = $.map.txtLayer,
	valLayer = $.map.valLayer,
	/*dzxLayer = $.map.dzxLayer,
	dzmLayer = $.map.dzmLayer,*/
	createMarkerSymbol = $.map.createMarkerSymbol;
	evaporationPanelDate.find('input[name="dayTime"]').val($.format.date(new Date(),'yyyy-MM-dd'));
	$('#queryEvaporationBtn').on('click',function(){
		queryRainData();
	});
	$('#searchEvaporationStatBtn').on('click',function(){
		var searchKey = $('#searchEvaporationStatInput').val();
		var existsCls = evaporationDataTable.find('tbody tr.filterTr');
		var findTrs;
		if(existsCls.length>0){
			findTrs = existsCls;
		}else{
			findTrs = evaporationDataTable.find('tbody tr').not(':hidden');
			findTrs.addClass('filterTr');
		}
		var hideTr = findTrs.filter(':not(:contains("'+searchKey+'"))');
		var showTr = findTrs.filter(':contains("'+searchKey+'")');
		if(searchKey!=""){
			$.each(showTr,function(index,item){
				$(item).find('td:eq(0)').text(parseInt(index)+1);
			});
		}
		showTr.show();
		hideTr.hide();
	});
	
	function queryRainData(){
		var dayTime,hourTime;
		hourTime=hourTime_vertical.val();
		dayTime=evaporationPanelDate.find('input[name="dayTime"]').val();
		dayTime=new Date(dayTime).setHours(hourTime, 0, 0, 0);
		
		tm=$.format.date(dayTime,"yyyy-MM-dd HH:mm");
		
		drawMapPoint(tm);
	}
	
	function drawMapPoint(tm){
		layer.clear();
		txtLayer.clear();
		valLayer.clear();
		/*dzxLayer.hide();
		dzmLayer.hide();*/
		map.graphics.clear();
		
		evaporationDataTable.find('tbody *').remove();
		$.get('../gis/EvaporationScreen.json?method=getLatestRiverGisInfo',{
			tm:tm,r:new Date().getTime()
		},function(data){
			var graphics = [];
			var dataIntArr = [];
			if(data.length>0){
				var evaporationDataTBody = evaporationDataTable.find('tbody');
				var addIndex = 0;
				var sizeItem = data.length;
				var isChoose = outSide.prop('checked');
				var csvDatas = '序号,站名,数值||';
				$.each(data, function(index,item) {
					var lgtd = parseFloat(item.lgtd);
					var lttd = parseFloat(item.lttd);
					var attr = {
						stcd : item.stcd,
						stnm : item.stnm,
						val : item.val,
						inProvOrInCity : item.inProvOrInCity,
						lgtd : lgtd,
						lttd : lttd,
						callType : 'evaporation'
					};
					var csvData = (index + 1)+','+attr.stnm+','+attr.val;
					if(index + 1 == sizeItem){
						csvDatas += csvData;
					}else{
						csvDatas += csvData+'||';
					}
					var valTextSymbol =new esri.symbol.TextSymbol(item.val);
					valTextSymbol.yoffset = 8;
					var textSymbol = new esri.symbol.TextSymbol(item.stnm+'('+item.val+')');
					textSymbol.yoffset = 8;
					
					var point =  new esri.geometry.Point(lgtd,lttd,map.spatialReference);
					var pointGraphic = new esri.Graphic(point, null, attr);
					var pointValGraphic = new esri.Graphic(point, valTextSymbol);
					var pointTxtGraphic = new esri.Graphic(point, textSymbol);
					
					var tr = $('<tr><td class="firstTd"></td><td>'+attr.stnm+'</td><td>'+attr.val+'</td></tr>');
					tr.data('lgtd',attr.lgtd).data('lttd',attr.lttd).data('val',attr.val).data('stnm',attr.stnm).data('stcd',attr.stcd);
					if(!isChoose && item.inProvOrInCity == 0){
						pointGraphic.hide();
						pointTxtGraphic.hide();
						pointValGraphic.hide();
						tr.hide();
					}else{
						pointGraphic.show();
						pointTxtGraphic.show();
						pointValGraphic.show();
						tr.find('td:eq(0)').text(++addIndex);
					}
					evaporationDataTBody.append(tr);
					
					layer.add(pointGraphic);
					txtLayer.add(pointTxtGraphic);
					valLayer.add(pointValGraphic);
				});
				evaporationDataTable.data('csvDatas',csvDatas);
				
				var renderer = new esri.renderer.SimpleRenderer(
						createMarkerSymbol());
				layer.setRenderer(renderer);
				layer.redraw();
				txtLayer.hide();
			}else{
				for(var i=0;i<5;i++){
					evaporationDataTable.find('tbody').append('<tr style="height:22px"><td></td><td></td><td></td></tr>');
				}
			}
			$.map.reExtent();
		});
	}
	
	outSide.on('change',function(){
		var isChoose = $(this).prop('checked');
		var addIndex = 0;
		var tableBodyTrs = evaporationDataTable.find('tbody tr');
		var tableBodyFirstTds = evaporationDataTable.find('.firstTd');
		evaporationDataTable.find('.filterTr').removeClass('filterTr');
		$('#searchEvaporationStatInput').val('');
		$.each(layer.graphics,function(index,item){
			var visible = item.visible;
			var inProvOrInCity = item.attributes.inProvOrInCity;
			var val = item.attributes.val;
			
			if(isChoose || (visible && inProvOrInCity == 1)){// 选中省外
				item.show();
				txtLayer.graphics[index].show();
				valLayer.graphics[index].show();
				var td = tableBodyFirstTds.eq(index);
				td.text(++addIndex);
				td.parent().show();
			}else if((!isChoose && inProvOrInCity == 0)|| !visible){// 未选中省外
				item.hide();
				txtLayer.graphics[index].hide();
				valLayer.graphics[index].hide();
				tableBodyTrs.eq(index).hide();
			}
		});
	});
	
	var graphicHighlight;
	var highlightPointVal;
	
	evaporationDataTable.find('tbody').on('click','tr',function(){
		var $_this = $(this);
		evaporationDataTable.find('.tableTrColor').removeClass('tableTrColor');
		$_this.addClass('tableTrColor');
		
		if($.isEmptyObject($_this.data())){
			return;
		}
		
		var lgtd = $_this.data('lgtd');
		var lttd = $_this.data('lttd');
		var stcd = $_this.data('stcd');
		var stnm = $_this.data('stnm');
		highlightPointVal = $_this.data('val');
		var point =  new esri.geometry.Point(lgtd,lttd,map.spatialReference);
		var attr = {
			stcd : stcd,
			stnm : stnm,
			val : highlightPointVal,
			callType : 'evaporation'
		};
		var graphic = new esri.Graphic(point, null);
		map.centerAndZoom(graphic.geometry,10);
		
		if(graphicHighlight){
			map.graphics.remove(graphicHighlight);
		}
		
		var highlight = new esri.symbol.SimpleMarkerSymbol(
				  esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 22,
				  new esri.symbol.SimpleLineSymbol(
					esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color('#ff0000'), 3.5),
					new dojo.Color([0, 0, 255, 0.01]
				  )
			    );
		graphicHighlight = new esri.Graphic(graphic.geometry, highlight,attr);
		map.graphics.add(graphicHighlight);
	});
});